package vivace.view;

import java.awt.*;
import vivace.helper.GUIHelper;

public class TrackBar extends SelectionArea {

	private static final long serialVersionUID = 8985934342192084275L;
	private String name;
	private int index, width, height;
	private Color color;
	private boolean selected;
	private boolean isDragged;

	/**
	 * Constructor for a trackbar
	 * @param index		sets the index for the trackBar
	 * @param name		what text should be written in the trackBar
	 * @param color		the color of the trackbar
	 */
	public TrackBar( int index, String name, Color color ){
		setOpaque(false);
		this.color = color;
		selected = false;
		this.index = index;
		this.name = name;
		height = GUIHelper.TRACK_HEIGHT;
		isDragged = false;
		
	}
	
	/**
	 * Checks if a trackBar is dragged by user
	 * @return
	 */
	public boolean getIsDragged(){
		return isDragged;
	}

	/**
	 * Sets the status of the trackBar that a user is dragging it right now
	 * @param value
	 */
	public void setIsDragged( boolean value ){
		isDragged = value;
	}
	
	/**
	 * Get the index for the trackBar
	 * @return
	 */
	public int getIndex(){
		return index;
	}
	
	/**
	 * Sets the width of the trackBar
	 * @param width
	 */
	public void setWidth(int width){
		this.width = width;
		setPreferredSize(new Dimension(width,height));
		repaint();
	}

	/**
	 * Paint up the trackBar
	 */
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		if(selected) {
			g.setColor(color);
			g.fillRect(0, 2, width-2, height-3);
			g.setColor(Color.BLACK);
			g.fillRect(2, 4, width-4, height-7);
			g.setColor(Color.WHITE);
			g.drawString(name,5,(int) Math.ceil(height/2) + 4 );	
		} else {
			g.setColor(Color.BLACK);
			g.drawRect(1, 3, width-2, height-5);
			g.setColor(color);
			g.fillRect(0, 2, width-2, height-4);
			g.setColor(Color.BLACK);
			g.drawRect(0, 2, width-2, height-5);
			g.drawString(name,5,(int) Math.ceil(height/2) + 4 );
		}
	}
	
	/**
	 * Get the selected trackBar
	 * @return
	 */
	public boolean getSelected(){
		return selected;
	}

	/**
	 * Set the trackBar as selectedc
	 * @param selected
	 */
	public void setSelected( boolean selected ){
		this.selected = selected;
		repaint();
	}
	
	/**
	 * Set the position of the trackBar
	 * @param x
	 * @param y
	 */
	public void setPosition( int x, int y ){
		setBounds(x,y,width,height);
	}

	/**
	 * Increases the trackbar's x-position by x pixels. 
	 * The input parameter can be negative.
	 * @param x
	 */
	public void moveByX( int x ){
		Rectangle bounds = this.getBounds();
		moveToX(bounds.x + x);
	}
	
	/**
	 * Moves the track bar to the x-position
	 * @param x
	 */
	public void moveToX( int x ){
		Rectangle bounds = this.getBounds();
		if( x < 1 ) {
			x = 1;
		}
		setBounds( x, (int) bounds.getY(), width, height);
	}
		
}
